package com.shangqing.web.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.shangqing.common.core.domain.AjaxResult;
import com.shangqing.system.droughtinformation.entity.*;
import com.shangqing.system.droughtinformation.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


import java.util.List;

@RestController
@RequestMapping("/droughtinformation")
@Api(tags = "干旱", description = "干旱相关接口")
public class DroughtInformationController {

    @Autowired
    private IdroughtInformationService idroughtInformationService;
    /**
     * 反馈信息
     */
    @Autowired
    private IdroughtFeedbackInfomationService idroughtFeedbackInfomationService;
    /**
     * 干旱类型
     */
    @Autowired
    private IdryTypeService idryType;
    /**
     * 干旱等级
     */
    @Autowired
    private IdroughtGradeService idroughtGrade;
    /**
     * 星期
     */
    @Autowired
    private IdroughtWeeksService idroughtWeeksService;
    /**
     * 城市占比
     */
    @Autowired
    private IdroughtCityService idroughtCityService;
    /**
     * 干旱预警信息
     */
    @Autowired
    private IdroughtWarningService idroughtWarningService;

    /**
     * 全区每年灾害数
     */
    @Autowired
    private IdistrictDroughtService idistrictDroughtService;
    /**
     * 植被相关信息
     */
    @Autowired
    private IvegetationService ivegetationService;

    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/selectdroughtInformation")
    @ApiOperation(value = "灾情监测预警模块的干旱信息", notes = "", httpMethod = "GET")
    public AjaxResult seldroughtInformation() {
        List<Droughtinformation> droughtinformations = null;
        try {
            droughtinformations = (List<Droughtinformation>) idroughtInformationService.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(droughtinformations);
        return AjaxResult.success("查询成功", droughtinformations);
    }

    @GetMapping("/selectdroughtFeedInformation")
    @ApiOperation(value = "灾情监测预警模块的干旱反馈信息", notes = "", httpMethod = "GET")
    public AjaxResult seldroughtFeedbackInformation() {
        List<DroughtBackInfimation> droughtBackInfimations = null;
        try {
            droughtBackInfimations = idroughtFeedbackInfomationService.getinfoservice();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", droughtBackInfimations);
    }

    @GetMapping("/selectdrytype")
    @ApiOperation(value = "灾情监测预警模块的干旱类型信息", notes = "", httpMethod = "GET")
    public AjaxResult seldrtytype() {
        List<DryType> dryTypes = null;
        try {
            dryTypes = idryType.getinfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", dryTypes);
    }

    @GetMapping("/selectDroughtGrade")
    @ApiOperation(value = "灾情监测预警模块的干旱等级信息", notes = "", httpMethod = "GET")
    public AjaxResult selDroughtGrade() {
        List<DroughtGrade> droughtGrades = null;
        try {
            droughtGrades = idroughtGrade.getinfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", droughtGrades);
    }

    @GetMapping("/selectDroughtWeeks")
    @ApiOperation(value = "灾情监测预警模块的本周降雨情况", notes = "", httpMethod = "GET")
    public AjaxResult selDroughtWeeks() {
        List<DroughtWeeks> droughtWeeks = null;
        try {
            droughtWeeks = idroughtWeeksService.getinfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", droughtWeeks);
    }

    @PostMapping("/selectDroughtCity/{year}")
    @ApiOperation(value = "灾情监测预警模块的每个城市的灾难数,占比,干旱情况", notes = "", httpMethod = "POST")
    public AjaxResult selDroughtCity(int year) {
        List<DroughtCity> droughtCities = null;
        try {
            droughtCities = idroughtCityService.getinfo(year);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", droughtCities);
    }

    @GetMapping("/selectDroughtCitySum")
    @ApiOperation(value = "灾情监测警模块的城市灾害总数", notes = "", httpMethod = "GET")
    public AjaxResult selDroughtCitySum() {
        int i = 0;
        try {
            i = idroughtCityService.getcitySum();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", i);
    }


    @GetMapping("/selectDroughtWarning")
    @ApiOperation(value = "灾情监测预警模块的预警信息", notes = "", httpMethod = "GET")
    public AjaxResult selDroughtWarning() {
        List<DroughtWarning> droughtWarnings = null;
        try {
            droughtWarnings = idroughtWarningService.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", droughtWarnings);
    }

    @GetMapping("/selecDistrictDrought")
    @ApiOperation(value = "灾情监测预警模块的全区每年灾害信息", notes = "", httpMethod = "GET")
    public AjaxResult selDistrictDrought() {
        List<DistrictDrought> districtDroughts = null;
        try {
            districtDroughts = idistrictDroughtService.getinfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", districtDroughts);
    }

    @GetMapping("/selecVegetation")
    @ApiOperation(value = "灾害风险监测模块的植被所有信息", notes = "", httpMethod = "GET")
    public AjaxResult selVegetation() {
        List<Vegetation> vegetations = null;
        try {
            vegetations = ivegetationService.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", vegetations);
    }

    @PostMapping("/selecVegetationByYear/{year}")
    @ApiOperation(value = "灾害风险监测模块的植被所有信息", notes = "", httpMethod = "POST")
    public AjaxResult selVegetationByYear(int year) {
        List<Vegetation> vegetations = null;
        try {
            vegetations = ivegetationService.getByYear(year);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", vegetations);
    }

    @PostMapping("/selecVegetationByName/{name}")
    @ApiOperation(value = "灾害风险监测模块的植被所有信息", notes = "", httpMethod = "POST")
    public AjaxResult selVegetationByName(String name) {
        List<Vegetation> vegetations = null;
        try {
            vegetations = ivegetationService.getByName(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", vegetations);
    }

    @PostMapping("/selecVegetationById/{id}")
    @ApiOperation(value = "灾害风险监测模块的植被所有信息", notes = "", httpMethod = "POST")
    public AjaxResult selVegetationById(int id) {
        List<Vegetation> vegetations = null;
        try {
            vegetations = ivegetationService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("查询成功", vegetations);
    }

    @PostMapping("/lives/{cityid}")
    @ApiOperation(value = "实时天气", notes = "", httpMethod = "POST")
    public AjaxResult livesyinchuan(int cityid) {
        String weatherUrl =  "https://restapi.amap.com/v3/weather/weatherInfo?city="+cityid+"&key=ed8913582dbbc7b27152c793cfac49b7&extensions=base";
        ResponseEntity<String> weatherEntity = restTemplate.getForEntity(weatherUrl, String.class);
        String weatherBody = weatherEntity.getBody();
        JSONObject weatherObject = JSONObject.parseObject(weatherBody);
        System.out.println(weatherObject);
        return AjaxResult.success("查询成功", weatherObject);
    }
    @PostMapping("/forecast/{cityid}")
    @ApiOperation(value = "天气预报", notes = "", httpMethod = "POST")
    public AjaxResult forecastyinchuan(int cityid) {
        String weatherUrl =  "https://restapi.amap.com/v3/weather/weatherInfo?city="+cityid+"&key=ed8913582dbbc7b27152c793cfac49b7&extensions=all";
        ResponseEntity<String> weatherEntity = restTemplate.getForEntity(weatherUrl, String.class);
        String weatherBody = weatherEntity.getBody();
        JSONObject weatherObject = JSONObject.parseObject(weatherBody);
        System.out.println(weatherObject);
        return AjaxResult.success("查询成功", weatherObject);
    }
    @GetMapping("/meteorologicalelements")
    @ApiOperation(value = "气象要素", notes = "", httpMethod = "GET")
    public AjaxResult meteorologicalelements() {
        return AjaxResult.success("查询成功", null);
    }
}
